package com.Bigcong.window;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class WhiteHoseDiaryWindow extends JFrame{
    private JTextField jTextField;
    private JButton jButton;
    private JTextField jTextFieldShow;
    private JPanel jPanel;

    public WhiteHoseDiaryWindow(String title) throws HeadlessException {
        super(title);
        init();

}

    private void init(){
        jPanel = new JPanel();
        jTextField = new JTextField(20);
        jTextFieldShow = new JTextField(20);
        jButton = new JButton("submit");

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File diary = new File("./data/test.txt");
                if(!diary.exists()){
                    try {
                        diary.createNewFile();
                    } catch (IOException exception) {
                        exception.printStackTrace();
                    }
                }

                FileOperation operation = new FileOperationImpl();
                operation.fileWriter(diary,jTextField.getText(), true);
                jTextFieldShow.setText(operation.fileReader(diary));
            }
        });

        jPanel.add(jTextField);
        jPanel.add(jButton);
        jPanel.add(jTextFieldShow);

        setContentPane(jPanel);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(300,300);
        setLocationRelativeTo(null);
    }
}
